﻿package com.godpaper.controllers.mxml.ds
{
	import flash.display.*;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	import mx.core.UIComponent;
	import mx.effects.Tween;
	import mx.effects.easing.Quadratic;
	import mx.events.TweenEvent;
	
	public class ArrayExample extends UIComponent
	{
		public static var SW:Number         = 550;
		public static var SH:Number         = 100;
		public static var BLUE:uint         = 0X03A3F0;
		public static var BLUE_LIGHT:uint   = 0X81D1F7;
		public static var PURPLE:uint       = 0XF40095;
		public static var PURPLE_LIGHT:uint = 0XF97FCA;
		public static var GREY:uint         = 0XCCCCCC;
		public static var START_CELLS:int   = 25;
		public static var MAX_NODES:int     = 7;
		public static var RADIUS_OFFSET:int = 5;
		
		public var arr:Array;
		
		public var canvas0:UIComponent;
		public var canvas1:UIComponent;
		
		public var inserting:Boolean;
		
		public var tween:Tween;
		public var tweening:Boolean;
		
		public function ArrayExample()
		{
			addEventListener(Event.ADDED_TO_STAGE,function(event:Event):void
			{
				stage.scaleMode = StageScaleMode.NO_SCALE;
				stage.align = StageAlign.TOP_LEFT;
				stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHander);
			});
			arr = [];
			
			for (var i:int = 0; i < START_CELLS; i++)
			{
				var c:Cell = arr[i] = new Cell();
				this.addChild(c);
			}
			
			addChild(canvas0 = new UIComponent());
			addChild(canvas1 = new UIComponent());
			
			align();
			//drawLinks();
			
			tweening = false;
			
			inserting = false;
		}
		
		public function keyHander(e:KeyboardEvent):void
		{
			//I - insert node
			if (e.keyCode == 73)
			{
				//if (Node.NUM_NODES < MAX_NODES && !inserting)
				//{
					//var node:Node = new Node();
					//addChild(node);
					//node.insert();
					//inserting = true;
					//
					//node.radius = Node.NODE_RADIUS / 2;
					//node.draw();
				//}
			}
		}
		
		public function onCellRelease(cell:Cell):void
		{
			removeCell(cell);
			
			//if (node.inserted)
			//{
				//node.inserted = false;
				//inserting = false;
				//align();
			//}
			//else
				//removeCell(node);
		}
		
		public function removeCell(cell:Cell):void
		{
			for (var i:int = 0; i < arr.length; i++)
			{
				if (arr[i] == cell)
				{
					arr.splice(i, 1);
					break;
				}
			}
			
			cell.deconstruct();
			
			//pop()
			if (i == arr.length)
			{
				align();
				return;
			}
			else
				tweenCell(i - 1);
		}
		
		public function tweenCell(cellIndex:int):void
		{
			tweening = true;
			var cell:Cell = arr[cellIndex + 1];
			
//			tween = new Tween(cell, "x", Regular.easeOut, cell.x, cell.x - Cell.SIZE, 5);
			tween = new Tween(cell, "x","y", cell.x, cell.x - Cell.SIZE);
			tween.addEventListener(TweenEvent.TWEEN_END, onMotionFinished);
		}
		
		public function onMotionFinished(e:TweenEvent):void
		{
			tween.removeEventListener(TweenEvent.TWEEN_END, onMotionFinished);
			var cell:Cell = e.target as Cell;
			
			for (var i:int = 0; i < arr.length; i++)
			{
				if (arr[i] == cell)
				{
					if (i == arr.length - 1)
					{
						tweening = false;
						align();
						return;
					}
					break;
				}
			}
			tweenCell(i);
		}
		
		public function align():void
		{
			var xEdgeOffset:Number = (SW - arr.length * Cell.SIZE) / 2;
			for (var i:int = 0; i < arr.length; i++)
			{
				var cell:Cell = arr[i];
				cell.setPos(xEdgeOffset + (i * Cell.SIZE), SH / 2);
				//cell.draw();
			}
		}
	}
}